<?php

/**	
 *	object rating module
 *	
 *	module that renders an ajax rating system for objects
 *
 *	@author			pieter van peteghem <pieter@onedotonly.be>
 *
 *	@copyright		oneDot only <we@onedotonly.be>
 * 	@copyright		Copyright (c) 2010, oneDot only. All rights reserved.
 * 	@license		BSD License
 */


class SocialBlog extends MOD {


	/**
	 *	constructor
	 *	
	 *	add module vars and reference (multiple modules on 1 page are possible)	
	 *	@param		array ($vars)
	 */
	 
	function __construct( $vars ) {
		$this->_addvars( $vars );
		$this->_ref();
	}


	/**
	 * 	define output
	 *	this is the base for rendering the stars at the frontend
	 */
	 
	public function output() {
		
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();

		$this->addclass( $this->action );
		
		if ( $result ){
			if( is_array( $result ))
				$result = '<ul>' . implode( "\n", $result ) . '</ul>';
			
			return $this->parse( $result ) . $this->render_js();
		}
	}
	
	
	/**
	 * 	simple rating system for any active object
	 *	this action defines an output 
	 */
	public function filter_list() {
		global $DB, $Object;
		
		if( !$Object ) 
			$blogs	= $DB->filter_objects( 'social.blog', $this->vars->condt );
		
		else 
			$blogs	= $Object->ref_objects( 'social.blog', true, $this->vars->limit, 'social' );
		
		if( $blogs ) {
			shuffle( $blogs );
			foreach( $blogs as $Blog ) {
				$User 		= new USER( (int) $Blog->get( 'creator' ));
				$dformat	= (( $this->vars->dformat )? $this->vars->dformat : 'd/m/Y' );
				
				$vars[] = array( 
					'short' => $this->mynl2br( substr( $Blog->get( 'content' ), 0, $this->vars->txtlimit ) . '... ' ), 
					'long' => $this->mynl2br( $Blog->get( 'content' ))
				); 
				
				if( !$_GET[ 'det' ] || ( $_GET[ 'det' ] == 'me' && $Blog->get( 'creator' ) == $_SESSION[ 'uid' ] )) {
				
					$res[] = '<li class="blog-post">';
					$res[] = '<h4 class="blog-title">' . $Blog->get( 'name' ) . '</h4>';
					$res[] = '<span class="blog-content">' . $this->mynl2br( substr( $Blog->get( 'content' ), 0, $this->vars->txtlimit )) . '... </span>';
					$res[] = '<span class="blog-author"> ' . $User->get( 'fname' ) . ' ' . $User->get( 'lname' ) . '</span>';
					$res[] = '<span class="blog-time">' . date( $dformat, strtotime( $Blog->get( 'llog' ))) . '</span>';
					$res[] = '<span class="stable"></span>';
					$res[] = '</li>';
				}
			}
			
			$this->js = "
				mod = new SocialBlog( " . json_encode( $vars ) . " );
				mod.expand();
			";
			return $res;
		}
	}
	
	
	public function filter_teaser() {
		global $DB, $Object;
	
		if( $Object && $Object->type == 'cal.single' && !$Object->get( 'date' ) > date( 'Y-m-d H:i:s' )) {
			$dformat	= (( $this->vars->dformat )? $this->vars->dformat : 'd/m/Y' );
			$message 	= $this->unshift_content();
	
			$res[] = '<li class="blog-post">' . $message;
			$res[] = '</li>';
			$res[] = '<li class="blog-post">';
			$res[] = '<a href="' . $this->vars->url . '?id=' . $Object->id . '&name=' . $Object->get( 'name' ) . '" title="' . $Object->get( 'name' ) . '">' . $this->vars->urltext . '</a>';
			$res[] = '</li>';
		
			return $res;
		}
	}
	
	
	private function mynl2br($text) { 
		return preg_replace("/\r\n|\n|\r/", trim( '<br />' ), $text); 
	} 
	
	
}


